package com.live.dengdengtv.activity.my;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.DateUtils;
import com.live.dengdengtv.utils.Notice;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by leiyun on 2016/8/11.
 */
public class ReceiveRecordActivity extends BaseActivity {

    private ListView receive_record_lv;
    private ArrayList<ItemDataBean> dataList = new ArrayList<ItemDataBean>();
    private RecordAdapter adapter;

    private double count = 0;//累计领取
    private TextView count_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_record);
        count_tv = (TextView)findViewById(R.id.count_tv);

        receive_record_lv = (ListView)findViewById(R.id.receive_record_lv);

        adapter = new RecordAdapter();
        receive_record_lv.setAdapter(adapter);

        exchangeVirTicket();
    }


    private void exchangeVirTicket(){
        HashMap<String,String> params = new HashMap<String, String>();
        params.put("page","1");
        params.put("page_count","10");
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.exchange_record, params,1);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                JsonArray array = response.data.getAsJsonArray();
                if(array.size()<=0) return false;

                for (int i=0; i<array.size() ;i++){
                    ItemDataBean liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), ItemDataBean.class);
                    dataList.add(liveItem);

                    try {
                        count = count+Double.parseDouble(liveItem.cash_count);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
                count_tv.setText("累计领取： "+count+"元");
            }else {
                Toast.makeText(context,"暂无领取记录",Toast.LENGTH_SHORT).show();
            }
        }
        return false;
    }

    public class RecordAdapter extends BaseAdapter{

        ViewHolder holder;
        public RecordAdapter(){

        }
        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView==null){
                LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
                convertView = mInflater.inflate(R.layout.receive_record_item,null);
                holder = new ViewHolder();
                holder.money_tv = (TextView)convertView.findViewById(R.id.money_tv);
                holder.date_tv = (TextView)convertView.findViewById(R.id.date_tv);
                holder.state_tv = (TextView)convertView.findViewById(R.id.state_tv);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }

            holder.money_tv.setText(dataList.get(position).cash_count);
            long date = Long.parseLong(dataList.get(position).create_at)*1000;//PHP返回时间戳  精确到秒,java精确到毫秒
            holder.date_tv.setText(DateUtils.dateToString(new Date(date),"yyyy年M月d日"));//可能有BUG---服务器返回的时间好像有问题
            holder.state_tv.setText("领取成功");

            return convertView;
        }
    }

    private static class ViewHolder{
        private TextView money_tv;
        private TextView date_tv;
        private TextView state_tv;
    }
    private static class ItemDataBean{
        public String cash_count;
        public String create_at;
        public String status;
        public String virtual_coin_count;
        public String virtual_ticket_count;
    }


}
